home *** CD-ROM | disk | FTP | other *** search
- Path: thor.tu.hac.com!collins
- From: collins@thor.tu.hac.com (Ron Collins)
- Newsgroups: comp.lang.c
- Subject: Re: revised code of calling a function twice from printf
- Date: 6 Mar 1996 00:32:08 GMT
- Organization: Advanced Depot Systems
- Message-ID: <4hime8$hh5@hacgate2.hac.com>
- References: <4hfs54$k4e@newsbf02.news.aol.com>
- NNTP-Posting-Host: thor.tu.hac.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- Razine (razine@aol.com) wrote:
- : Here is the actual code , modified with everyone's suggestions. However
- : there is still a bug in here somewhere. Can anyone please explain what is
- : wrong with this.
-
- : It returns
-
- : [1] NONE [2] NE
-
- : I would like it to return
-
- : [1] NONE [2] Asprin
-
- : Thank you very much for your help. it is greatly appreciated.
-
-
- : #include <stdio.h>
- : #include <string.h>
-
- : char *display_drug_type(int drug_index);
-
- : int drug_inventory[5]={0,1,0,0,0};
-
- : int main() {
-
- : printf("[1] %s [2] %s
- : \n",display_drug_type(0),display_drug_type(1));
-
- : return 0;
- : }
-
- : char *display_drug_type(int drug_index) {
- : char drug_type[81]="\0";
-
- : switch(drug_inventory[drug_index]) {
- : case 0 : strcpy(drug_type,"NONE"); break;
- : case 1 : strcpy(drug_type,"Asprin"); break;
- : default : printf("Error in Display_drug_inventory");
- : }
- : return drug_type;
- : }
-
- It ain't gonna happen.
-
- Mainly, because array "drug_type" is transient; that is, the array
- "disappears" in between calls to "display_drug_type", which means that
- the "char *" you are returning from the function call is meaningless.
-
- You might consider using a "static" array to maintain a valid "char *",
- but this would obviously be incorrect also (for your purposes).
-
-
- -- collins --
-
-
-